package inet.app.vod.net.http;

import inet.app.vod.config.AppConstants;
import inet.app.vod.object.User;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.inet.app.framework.util.Log;
import com.inet.app.framework.util.text.DateUtil;
import com.inet.app.framework.util.text.StringUtil;

public class VolleyClient {
	private static final String BASE_URL = "http://iphim.vn/json.aspx";
	private static final String BASE_URL_TOKEN = "http://"
			+ AppConstants.SERVER_HOST + ":" + AppConstants.SERVER_TOKEN_PORT;
	private static final String CREATE_TOKEN = "/token/createtokenbased64?";
	private static final String ADD_USER = "/server/add_user";
	private static final String DELETE_USER = "/server/del_user";
	private static final String NAME_REQ = "name";
	private static final String PARAM_SIGNIN = "REQ_SIGNIN";
	private static final String PARAM_USERNAME = "username";
	private static final String PARAM_PASSWORD = "password";
	private static final String PARAM_EMAIL = "email";
	private static final String PARAM_LOGIN = "REQ_LOGIN";
	private static final String PARAM_CATEGORY = "REQ_CATEGORIES";
	private static final String PARAM_HOME_ID = "HOME_ID";
	private static final String PARAM_LIST_HOT = "REQ_LIST_HOT";
	private static final String PARAM_CATEGORY_ID = "CATEGORY_ID";
	private static final String PARAM_REQ_ITEMS = "REQ_CATEGORY";
	private static final String PARAM_PAGE = "PAGE";
	private static final String PARAM_PAGE_SIZE = "PAGE_SIZE";
	private static final String PARAM_REQ_DETAILS = "REQ_DETAIL";
	private static final String PARAM_ITEM_ID = "ID";
	private static final String PARAM_ENCRYPT = "encrpty";
	private static final String PARAM_TOKEN = "token";
	private static final String PARAM_PROPOSED = "REQ_PROPOSED";
	private static final String PARAM_SEARCH = "REQ_SEARCH";
	private static final String PARAM_KEYWORD = "keyword";

	public static void login(String usrName, String pwd, VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(NAME_REQ, PARAM_LOGIN);
		params.put(PARAM_USERNAME, usrName);
		params.put(PARAM_PASSWORD, pwd);
		get(BASE_URL, params, volley);
	}

	public static void register(String usrName, String pwd, String email,
			VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(NAME_REQ, PARAM_SIGNIN);
		params.put(PARAM_USERNAME, usrName);
		params.put(PARAM_PASSWORD, pwd);
		params.put(PARAM_EMAIL, email);
		get(BASE_URL, params, volley);
	}

	public static void requestCategories(String homeId, VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(NAME_REQ, PARAM_CATEGORY);
		params.put(PARAM_HOME_ID, homeId);
		get(BASE_URL, params, volley);
	}

	public static void requestListHot(String homeId, String categoryId,
			VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(NAME_REQ, PARAM_LIST_HOT);
		params.put(PARAM_HOME_ID, homeId);
		params.put(PARAM_CATEGORY_ID, categoryId);
		get(BASE_URL, params, volley);
	}

	public static void requestItems(String homeId, String categoryId,
			int pageSize, int page, VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(NAME_REQ, PARAM_REQ_ITEMS);
		params.put(PARAM_HOME_ID, homeId);
		params.put(PARAM_CATEGORY_ID, categoryId);
		params.put(PARAM_PAGE_SIZE, String.valueOf(pageSize));
		params.put(PARAM_PAGE, String.valueOf(page));
		get(BASE_URL, params, volley);
	}

	public static void requestDetails(String homeId, String itemId,
			VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(NAME_REQ, PARAM_REQ_DETAILS);
		params.put(PARAM_HOME_ID, homeId);
		params.put(PARAM_ITEM_ID, itemId);
		get(BASE_URL, params, volley);
	}

	public static void requestToken(String usrName, String pwd,
			VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		String encrypt = StringUtil.encodeBase64(usrName + ":" + pwd);
		params.put(PARAM_ENCRYPT, encrypt);
		String baseUrl = BASE_URL_TOKEN + CREATE_TOKEN;
		get(baseUrl, params, volley);
	}

	public static void requestRootToken(VolleyListener volley) {
		requestToken(AppConstants.ROOT_NAME, AppConstants.ROOT_PASS, volley);
	}

	public static void requestSuggests(String homeId, String id,
			VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(NAME_REQ, PARAM_PROPOSED);
		params.put(PARAM_HOME_ID, homeId);
		params.put(PARAM_ITEM_ID, id);
		get(BASE_URL, params, volley);
	}

	public static void requestNewUser(String rootToken, String userName,
			String pass, VolleyListener volley) {
		SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		String tomorrow = df.format(DateUtil.createNextDate());
		Map<String, String> params = new HashMap<String, String>();
		params.put(PARAM_TOKEN, rootToken);
		params.put(PARAM_USERNAME, userName);
		params.put(PARAM_PASSWORD, pass);
		params.put("group", "golden");
		params.put("paymodel", "free");
		params.put("user_point", "2000");
		params.put("expired_time", tomorrow);
		String baseUrl = BASE_URL_TOKEN + ADD_USER;
		get(baseUrl, params, volley);
	}

	public static void deleteUser(String token, String userName,
			VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(PARAM_TOKEN, token);
		params.put(PARAM_USERNAME, userName);
		String baseUrl = BASE_URL_TOKEN + DELETE_USER;
		get(baseUrl, params, volley);
	}

	public static void requestSearch(String homeId, String keyword,
			VolleyListener volley) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(NAME_REQ, PARAM_SEARCH);
		params.put(PARAM_HOME_ID, homeId);
		params.put(PARAM_KEYWORD, keyword);
		get(BASE_URL, params, volley);
	}

	public static void editAccount(User userEdit, VolleyListener volley) {
		if (userEdit != null) {
			Map<String, String> params = new HashMap<String, String>();
			params.put(NAME_REQ, "REQ_EDIT_ACCOUNT");
			params.put("password", userEdit.getPassword());
			params.put("email", userEdit.getEmail());
			params.put("full_name",
					userEdit.getFullname().replaceAll(" ", "%20"));
			params.put("gender", String.valueOf(userEdit.getGender()));
			params.put("phone", userEdit.getPhone());
			params.put("user_id", userEdit.getId());
			get(BASE_URL, params, volley);
		}
	}

	public static void downloadSub(String url, VolleyListener volley) {
		get(url, null, volley);
	}

	/*********************************** Default function ******************************/
	protected static void get(String url, Map<String, String> params,
			VolleyListener volley) {
		String uri = buildParams(url, params);
		Log.d("request = " + uri);
		StringRequest request = new StringRequest(Method.GET, uri,
				volley.getSuccessListener(), volley.getErrorListener());
		boolean isNetwork = volley.start();
		if (isNetwork) {
			MyVolley.getRequestQueue().add(request);
		}
	}

	protected static String buildParams(String url, Map<String, String> params) {
		if (params == null || params.size() == 0) {
			return url;
		}
		if (!url.endsWith("?")) {
			url += "?";
		}
		Set<String> keys = params.keySet();
		Object value;
		String endChar;
		StringBuffer request = new StringBuffer();
		int len = keys.size();
		int i = 0;
		for (String key : keys) {
			value = params.get(key);
			endChar = (i == (len - 1)) ? "" : "&";
			request.append(key).append("=").append(value).append(endChar);
			i++;
		}
		return url + request.toString();
	}

}
